package com.qf.jie.dao.impl;

import com.qf.jie.dao.CommodityDao;
import com.qf.jie.entity.po.Commodity;
import com.qf.jie.utils.DBUtils;

import java.util.List;

public class CommodityDaoImpl implements CommodityDao {
//    public static void main(String[] args) {
//        //Commodity commodity = new Commodity(1, "败家之眼", "httpxxxxxx8080/img", 5000, 4999.0, 2);
//        CommodityDaoImpl commodityDao = new CommodityDaoImpl();
//    }
    @Override
    public int addBean(Commodity commodity) {
        return DBUtils.excuteUpdate("insert into commodity(name, img_url, stock, price, user_id) values(?,?,?,?,?)",
                commodity.getName(), commodity.getImg_url(), commodity.getStock(), commodity.getPrice(), commodity.getUser_id());
    }

    @Override
    public int deleteById(int id) {
        return DBUtils.excuteUpdate("delete from commodity where id=?", id);
    }
    @Override
    public int deleteByUserId(int user_id) {
        return DBUtils.excuteUpdate("delete from commodity where user_id=?", user_id);
    }

    @Override
    public List<Commodity> getCommodityListByUserId(int user_id) {
        return DBUtils.excuteQuery(Commodity.class, "select * from commodity where user_id = ?",
                user_id);
    }

    @Override
    public int updateById(Commodity commodity) {
        return DBUtils.excuteUpdate("update commodity set name=?,img_url=?,stock=?,price=?,user_id=? where id=?",
                commodity.getName(), commodity.getImg_url(), commodity.getStock(), commodity.getPrice(), commodity.getUser_id()
                ,commodity.getId());
    }
    @Override
    public int getCount() {
        return DBUtils.excuteQuery("select count(*) from commodity");
    }
    @Override
    public int getCountByName(String name) {
        return DBUtils.excuteQuery("select count(*) from commodity where name=?", name);
    }
    @Override
    public List<Commodity> getList() {
        return DBUtils.excuteQuery(Commodity.class, "select * from commodity");
    }
    @Override
    public List<Commodity> getList(int start, int len) {
        return DBUtils.excuteQuery(Commodity.class, "select * from commodity limit ?,?", start, len);
    }
    @Override
    public List<Commodity> getListLike(String name) {
        name = "%" + name +"%";
        return DBUtils.excuteQuery(Commodity.class, "select * from commodity where name like ?", name);
    }
    @Override
    public List<Commodity> getBeanById(int id) {
        return DBUtils.excuteQuery(Commodity.class, "select * from commodity where id=?", id);
    }


    @Override
    public int deleteByIdAndUserId(int id, int user_id) {
        return DBUtils.excuteUpdate("delete from commodity where id=? and user_id=?", id, user_id);
    }

    @Override
    public int getIdByNameAndPassword(String name, String password) {
        return 0;
    }
    @Override
    public int shamDeleteById(int id) {
        return 0;
    }

    @Override
    public int getIdByName(String name) {
        return 0;
    }
}
